package com.tgb.util;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Random;
import java.util.UUID;

/**
 * Created by admin on 2017-04-11.
 */
public class WebTools {

    public static String replaceStr(String str) {
        return str.replaceAll("'", "").replaceAll("\\*", "")
                .replaceAll("\\|", "").replaceAll("\\+", "")
                .replaceAll("=", "").replaceAll("-", "").replaceAll(" ", "")
                .replaceAll(";", "");
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取count个随机数
     *
     * @param count
     *            随机数个数
     * @return
     */
    public static String game(int count) {
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for (int i = 0; i < count; i++) {
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num) + ""), "");
        }
        return sb.toString();
    }

    public static int DoubleToInt(String num) {
        int price = 0;
        if (num.contains(".")) {
            String[] z = num.split("\\.");
            int a1 = Integer.parseInt(z[0]);
            int a2 = Integer.parseInt(z[1]);
            if (z[1].length() == 1) {
                price = a1 * 100 + a2 * 10;
            } else if (z[1].length() == 2) {
                price = a1 * 100 + a2;
            } else if (z[1].length() == 0) {
                price = a1 * 100;
            }
        } else {
            price = Integer.parseInt(num) * 100;
        }
        return price;
    }
    /**
     * 汉语转拼音 取首字母
     */
    // 返回中文的首字母
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        char word = str.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
        if (pinyinArray != null) {
            convert += pinyinArray[0].charAt(0);
        } else {
            convert += word;
        }
        return convert.toUpperCase();
    }

    /**
     * @return 生成12位随机数
     */
    public static String getRandom(){
        String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        int size = 12;
        StringBuffer sb = new StringBuffer();
        Random ran = new Random();
        for (int i = 1; i <= size; i++) {
            int r = ran.nextInt(chars.length);
            sb.append(chars[r]);// 将字符保存，存入Session
        }

        return sb.toString();
    }

    /**
     * @return 生成12位随机数
     */
    public static String getRandom(int size){
        String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        StringBuffer sb = new StringBuffer();
        Random ran = new Random();
        for (int i = 1; i <= size; i++) {
            int r = ran.nextInt(chars.length);
            sb.append(chars[r]);// 将字符保存，存入Session
        }

        return sb.toString();
    }
    public static void main(String[] args){
        System.out.println(getRandom());
    }
}
